﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using points.Assist;
using points.AppInfo;
using points.Model;
using System.Configuration;

namespace points
{
    public partial class Merchant : Helper
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckPageUsage(AppVars.customer_type["merchant"].Pk);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;

            //the individual receiving the points
            Customer consumer = new Customer();
            consumer.Select(this.txtUsername.Text);

            int merchantPk = (int)Session["CUSTOMER_PK"];

            Customer merchant = new Customer();
            merchant.Select(merchantPk);

            int numerator = merchant.ValPoint;
            int denominator = merchant.ValDollar;

            int employeePk = 3;//hardcoded for now.

            int transactionPk = Int32.Parse(this.ddlTransactionType.SelectedValue);

            int dollarValue = Int32.Parse(txtDollar.Text);
            //pointvalue should be calculated from the dollar/points column.
            int pointsValue = (int) dollarValue * numerator / denominator;

            string obj_Name = ConfigurationManager.AppSettings["OBJECT_CUSTOMER_TRANSACTION"];

            /*
                pk
                obj_name
                add_dt
                mod_dt
                marked_delete_y_n
                rec_id
                fk_customer_id
                fk_merchant_id
                fk_employee_id
                fk_transaction_id
                point_value
                msg
            */

            CustomerTransaction ct = new CustomerTransaction
            (
                0,
                obj_Name,
                now,
                now,
                "n",
                0,              //rec_id
                consumer.Pk,
                merchantPk,
                employeePk, 
                transactionPk,
                pointsValue,
                ""
            );

            ct.Insert();
        }
    }
}